package com.example.mytest6;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


public class MyFragment7 extends Fragment {
    private HorizontalBarChart hbar_2;

    public MyFragment7() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment7, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        //数据显示在图形内部
        hbar_2.setDrawValueAboveBar(false);
        hbar_2.setDescription(null);
        hbar_2.setScaleEnabled(false);
        hbar_2.getLegend().setEnabled(false);
        hbar_2.setExtraOffsets(20, 0, 20, 0);

        final List<String> xV = new ArrayList<>();
        xV.add("机动车逆向行驶");
        xV.add("违规使用专用车道");
        xV.add("违反禁令标志指示");
        xV.add("不按规定系安全带");
        xV.add("机动车不走机动车道");
        xV.add("违反信号灯规定");
        xV.add("违反禁止标线指示");
        xV.add("违规停车拒绝驶离");
        xV.add("违规驶入导向车道");
        xV.add("超速行驶");

        List<BarEntry> yV = new ArrayList<>();
        yV.add(new BarEntry(0, 3.37f));
        yV.add(new BarEntry(1, 3.87f));
        yV.add(new BarEntry(2, 4.31f));
        yV.add(new BarEntry(3, 4.80f));
        yV.add(new BarEntry(4, 4.82f));
        yV.add(new BarEntry(5, 7.43f));
        yV.add(new BarEntry(6, 9.97f));
        yV.add(new BarEntry(7, 12.87f));
        yV.add(new BarEntry(8, 22.10f));
        yV.add(new BarEntry(9, 26.46f));

        BarDataSet dataSet = new BarDataSet(yV, "");
        dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        int color[] = {0xFF94ADD1, 0xFFB0A0C2, 0xFFFBAF6A, 0xFFB4CB8C, 0xFFF9C201, 0xFF644878, 0xFFE56B0B, 0xFF79913F, 0xFF4E7EC0, 0xFFC40101};
        dataSet.setValueFormatter(new MyData7Formatter());
        dataSet.setHighlightEnabled(false);


        YAxis left = hbar_2.getAxisLeft();
        left.setEnabled(false);
        left.setDrawAxisLine(false);
        left.setDrawGridLines(false);
        left.setAxisMaximum(70);
        left.setAxisMinimum(0);
        left.setLabelCount(8, true);


        YAxis yAxis = hbar_2.getAxisRight();
        yAxis.setValueFormatter(new MyYAxis7Formatter());

        yAxis.setLabelCount(7, true);
        yAxis.setAxisMinimum(0);
        yAxis.setAxisMaximum(30);
        yAxis.setGranularity(1);


        XAxis xAxis = hbar_2.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setTextSize(14);
        xAxis.setLabelCount(9);
        xAxis.setValueFormatter(new MyXAxis7Formatter(xV));

        BarData data = new BarData(dataSet);
        data.setBarWidth(0.5f);
        hbar_2.setData(data);
    }

    class MyData7Formatter extends ValueFormatter {
        @Override
        public String getBarLabel(BarEntry barEntry) {
            return new DecimalFormat("0.00%").format(barEntry.getY() / 100);
        }
    }

    class MyYAxis7Formatter extends ValueFormatter {
        @Override
        public String getFormattedValue(float value) {
            return new DecimalFormat("0.00%").format(value / 100);
        }
    }

    class MyXAxis7Formatter extends ValueFormatter {
        List<String> xV = new ArrayList<>();

        public MyXAxis7Formatter(List<String> xV) {
            this.xV = xV;
        }

        @Override
        public String getFormattedValue(float value) {
            return xV.get((int) value);
        }
    }

    private void initView(View view) {
        hbar_2 = view.findViewById(R.id.hbar_2);
    }
}
